<?php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use App\Base\Annotation\Common;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ApiResource()
 * @ORM\Entity(repositoryClass="App\Repository\AdminGroupRepository")
 */
class AdminGroup
{
    /**
     * @Common(cnName="#",editType="text",listType="text")
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @Common(cnName="名称",editType="text",listType="text")
     * @ORM\Column(type="string", length=255)
     */
    private $name;

    /**
     * @Common(cnName="角色",editType="text",listType="text")
     * @ORM\Column(type="string", length=255)
     */
    private $role;

    /**
     * @Common(cnName="权限",editType="text",listType="text")
     * @ORM\ManyToMany(targetEntity="App\Entity\AdminMenu", inversedBy="groups")
     */
    private $menus;

    /**
     * @Common(cnName="用户",editType="text",listType="text")
     * @ORM\ManyToMany(targetEntity="App\Entity\AdminUser", mappedBy="groups")
     */
    private $users;

    public function __construct()
    {
        $this->menus = new ArrayCollection();
        $this->users = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    public function getRole(): ?string
    {
        return $this->role;
    }

    public function setRole(string $role): self
    {
        $this->role = $role;

        return $this;
    }

    /**
     * @return Collection|AdminMenu[]
     */
    public function getMenus(): Collection
    {
        return $this->menus;
    }

    public function addMenu(AdminMenu $menu): self
    {
        if (!$this->menus->contains($menu)) {
            $this->menus[] = $menu;
        }

        return $this;
    }

    public function removeMenu(AdminMenu $menu): self
    {
        if ($this->menus->contains($menu)) {
            $this->menus->removeElement($menu);
        }

        return $this;
    }

    /**
     * @return Collection|AdminUser[]
     */
    public function getUsers(): Collection
    {
        return $this->users;
    }

    public function addUser(AdminUser $user): self
    {
        if (!$this->users->contains($user)) {
            $this->users[] = $user;
            $user->addGroup($this);
        }

        return $this;
    }

    public function removeUser(AdminUser $user): self
    {
        if ($this->users->contains($user)) {
            $this->users->removeElement($user);
            $user->removeGroup($this);
        }

        return $this;
    }
}